package com.weibo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReturnInfo extends HttpServlet{
	private String url;
	private String user;
	private String password;
	
	public void init()throws ServletException{
		String driverName = this.getInitParameter("driverClass");
		url = this.getInitParameter("url");
		user = this.getInitParameter("user");
		password = this.getInitParameter("password");
		try{
			Class.forName(driverName);
		}catch(ClassNotFoundException ce){
			ce.printStackTrace();
			System.out.println("加载数据库驱动程序失败!");
		}
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
					throws ServletException, IOException{
		init();
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=UTF-8");
		PrintWriter out=resp.getWriter();
		String regName=req.getParameter("regName");
		Connection conn = null;
		PreparedStatement pstmt = null;
		try{
			out.write("eo");
			conn = DriverManager.getConnection(url, user,password);
			String sql="select * from personinfo where regName=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, regName);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsmt = rs.getMetaData(); 
			int iCount = rsmt.getColumnCount(); 
			System.out.println(iCount);
			while(rs.next()) { 
			for(int i=1;i<=iCount;i++) { 
					if (rsmt.getColumnName(i) != "password") {
						out.println(rsmt.getColumnLabel(i)+":");
						out.println(rs.getString(i));
					}
			} 
			}
			rs.close();
			pstmt.close();
			conn.close();
			} 
		catch (Exception e){
			System.out.println(e);
		} 
			} 
	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ 
			doGet(req,resp); 
			} 		
	}
